<!DOCTYPE html>
<html lang="zh_CN" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">

<!-- head 头部-->
<head th:replace="admin/layout/include::head('友情链接管理','')"></head>

<body>
<!-- loading 动画-->
<div th:replace="admin/layout/include::loading"></div>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form toolbar">
                <div class="pull-left layui-form-pane search-box">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">搜索：</label>
                        <div class="layui-input-inline mr0">
                            <input id="edtSearch" class="layui-input" type="text" placeholder="输入关键字"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button id="btnSearch" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
                    </div>
                </div>
            </div>


            <!-- 数据表格 -->
            <table class="layui-table" id="data-table" lay-filter="data-table"></table>
        </div>
    </div>
</div>
<!-- 表格状态列 -->
<script type="text/html" id="data-table-state">
    <input type="checkbox" lay-filter="updateState" ajax-url="/admin/link/updateState/" value="{{d.id}}" lay-skin="switch" lay-text="正常|禁用"
           {{d.state==0?'checked':''}}/>
</script>
<!-- 表格操作列 -->
<script type="text/html" id="data-table-bar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" ajax-event="del" ajax-url="/admin/link/" value="{{d.id}}">删除</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="modelLink">
    <form id="modelLinkForm" lay-filter="modelLinkForm" class="layui-form model-form no-padding">
        <input name="id" type="hidden"/>
        <div class="model-form-body" style="max-height:380px;">
            <div class="layui-form-item">
                <label class="layui-form-label required">友链名称:</label>
                <div class="layui-input-block">
                    <input name="name" placeholder="请输入名" type="text" class="layui-input" maxlength="20"
                           lay-verType="tips" lay-verify="required" required/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">URL:</label>
                <div class="layui-input-block">
                    <input name="url" type="text" class="layui-input" maxlength="50"
                           lay-verType="tips" lay-verify="required|url" required/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">联系QQ:</label>
                <div class="layui-input-block">
                    <input name="qq" type="text" class="layui-input" maxlength="11"/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">邮箱:</label>
                <div class="layui-input-block">
                    <input name="email" type="text" class="layui-input" maxlength="50"/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">LOGO:</label>
                <div class="layui-input-block">
                    <input name="logo" type="text" class="layui-input" maxlength="50"/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">排序</label>
                <div class="layui-input-block">
                    <input name="orderNo" placeholder="排序" type="number" class="layui-input"
                           lay-verify="number"/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">描述:</label>
                <div class="layui-input-block">
                    <input name="description" type="text" class="layui-input" maxlength="50"/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">备注:</label>
                <div class="layui-input-block">
                    <input name="remark" type="text" class="layui-input" maxlength="50"/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">状态:</label>
                <div class="layui-input-block">
                    <input type="checkbox" id="state" name="state" lay-skin="switch" value="0" checked lay-text="正常|禁用">
                </div>
            </div>
        </div>
        <div class="layui-form-item text-right model-form-footer">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closePageDialog">取消</button>
            <button class="layui-btn" lay-filter="modelSubmitLink" lay-submit>保存</button>
        </div>
    </form>
</script>
<script type="text/html" id="toolbarOepration">
    <div class="layui-btn-group">
        <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="add"><i
                class="layui-icon">&#xe654;</i>添加</button>
        <button class="layui-btn layui-btn-sm layui-btn-danger" ajax-event="batchDel" ajax-url="/admin/link/"><i class="layui-icon layui-icon-delete"></i>批量删除</button>
    </div>
</script>
<!-- js部分 -->
<div th:replace="admin/layout/include::js"></div>
</body>



<script>
    layui.use(['form', 'table', 'util', 'admin'], function () {
        var form = layui.form;
        var table = layui.table;
        var layer = layui.layer;
        var util = layui.util;
        var admin = layui.admin;

        var formUrl;  // 表单提交的action

        //渲染表格
        var tableIns = table.render({
            elem: '#data-table',
            url: '/admin/link/',
            page : true,
			loading : true,
			height : 'full-115',
            toolbar: '#toolbarOepration',
			limit : 10,
			limits : [ 10, 20, 50, 100 ],
            response:{
	        	countName: 'total'
	        },
            cols: [
                [
                {type: 'checkbox'},
                 /*{field: 'id', title: 'ID'},*/
                {field: 'name', title: '名称',width:200},
                {field: 'url', title: '地址URL',width:260},
                {field: 'logo',  title: 'LOGO'},
                {field: 'qq',title: '联系qq'},
                {field: 'email', title: '邮箱'},
                {field: 'orderNo',sort: true, title: '排序'},
                {field: 'state', title: '状态',templet:'#data-table-state'},
                {
                    field: 'createTime', sort: true, templet: function (d) {
                        return util.toDateString(d.createTime);
                    }, title: '创建时间'
                },
                {align: 'center', toolbar: '#data-table-bar', title: '操作'}
            ]
            ]
        });
        //头工具栏事件
        table.on('toolbar(data-table)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'add':
                    showEditModel();
                    break;
            };
        });

        // 工具条点击事件
        table.on('tool(data-table)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            }
        });

        // 搜索按钮点击事件
        $('#btnSearch').click(function () {
            var searchVal = $('#edtSearch').val();
            tableIns.reload( {where: {keyword: searchVal}},'data');
        });


        // 显示编辑弹窗
        var showEditModel = function (data) {
            admin.open({
                type: 1,
                title: data ? '修改' : '添加',
                area: ['500px', '520px'],
                content: $('#modelLink').html(),
                success:function(layero, index){
                    form.val('modelLinkForm', data);
                    // 表单提交事件
                    form.on('submit(modelSubmitLink)', function (data) {
                        layer.load(2);
                        $.post('/admin/link/', data.field, function (res) {
                            layer.closeAll('loading');
                            if (res.success) {
                                layer.close(index);
                                layer.msg(res.msg, {icon: 1});
                                tableIns.reload({}, 'data');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'json');
                        return false;
                    });
                }
            });

        };


    });

</script>
</html>